<?php

namespace App\Logic;

use \Common\Logic\BaseLogic;

class RecommLogic extends BaseLogic
{
    private $userModel;
    private $incomeModel;
    private $recomConfModel;

    public function __construct()
    {
        parent::__construct();
        $this->userModel = D('User');
        $this->incomeModel = D("Income");
        $this->recomConfModel = D("Recommconf");
    }

    /**
     * 获取资金分成比例配置
     *
     * @return void
     */
    public function getRecommConf()
    {
        return $this->recomConfModel->find();
    }

    /**
     * 获取分享收益
     * @param $userId
     * @return mixed
     */
    public function getIncomeSum($userId)
    {
        if (empty($userId)) {
            return static::$res->err("userId不能为空");
        }
        $where = array(
            "task_type" => 1,
            "user_id" => $userId
        );
        $sum = $this->incomeModel->where($where)->sum("income");
        return static::$res->suc("成功", $sum);
    }


    /**
     * 获取收徒数
     * @param $userId
     * @return mixed
     */
    public function getRecommNum($userId)
    {
        if (empty($userId)) {
            return static::$res->err("userId不能为空");
        }

        $where = array(
            "parent_id" => $userId,
            "state" => array('neq', 0)
        );
        $count = $this->userModel->where($where)->count();
        return static::$res->suc("成功", $count);
    }

    /**
     * 获取推荐的列表
     * @param int $page
     * @param int $size
     * @param string $userId
     * @return mixed
     */
    public function getRecommList($page = 1, $size = 20, $userId = '')
    {
        if (empty($userId)) {
            return static::$res->err("userId不能为空");
        }

        $where = array(
            "parent_id" => $userId,
            "state" => array('neq', 0)
        );

        $list = $this->userModel->where($where)->page($page . "," . $size)->order('id desc')->select();
        $count = $this->userModel->where($where)->count();

        return static::$res->suc("成功", array(
            "list" => $list,
            "count" => $count
        ));
    }
}
